/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				SequentialCondition
 
Function						: 				Provides a class for holding a sequential 
												condition 
****************************************************************************/
package planninglanguage.conditions;

import org.jdom.Element;

import util.XMLWriter;
import base.WargusGameState;

public class SequentialCondition extends Condition {

	Condition m_c1,m_c2;
	int m_status;

	public SequentialCondition(Condition a_c1,Condition a_c2)
	{
		m_c1=a_c1;
		m_c2=a_c2;
		m_status=0;
	}
	
	public void setStatus(int s) {
		m_status = s;
	}
	
	public void reset()
	{
		m_status = 0;
		m_c1.reset();
		m_c2.reset();
	}
	
	public boolean test(WargusGameState a_gameState)
	{
		if (m_status==0) {
			if (m_c1.test(a_gameState)) m_status = 1;
		}
		if (m_status==1) {
			if (m_c2.test(a_gameState)) {				
				m_status = 2;
				return true;
			}
		}
		return false;
	}

	public String toString(int tabs) {
		String str= super.toString(tabs);
		int i;
		
		for(i=0;i<tabs;i++) str = str + "  ";
		str = str +  "Sequential( status = \n" + m_status;
		if (m_c1!=null)	{
			str = str + m_c1.toString(tabs+1) + "\n";
		} else {
			for(i=0;i<tabs+1;i++) str = str + "  ";
			str = str +  "null\n";
		}  
		if (m_c2!=null)	{
			str = str + m_c2.toString(tabs+1) + "\n";
		} else {
			for(i=0;i<tabs+1;i++) str = str + "  ";
			str = str +  "null\n";
		}  
		for(i=0;i<tabs;i++) str = str + "  ";
		str = str +  ")";
		return str;
	}	
	
	public void writeXML(XMLWriter w) {
		w.tag("condition type=\""+this.getClass().getSimpleName()+"\"");
		{
			w.tag("status", m_status);
			m_c1.writeXML(w);
			m_c2.writeXML(w);
		}
		w.tag("/condition");
	}
	
	protected static Condition loadConditionFromXML(Element e) {
		Condition a = Condition.loadFromXML((Element) e.getChildren().get(0));
		Condition b = Condition.loadFromXML((Element) e.getChildren().get(1));
		SequentialCondition c = new SequentialCondition(a, b);
		c.setStatus(Integer.parseInt(e.getChildText("status")));
		return c;
	}
}
